package org.liquidByte.trafficMustang.androidApp.activities;

import org.liquidByte.trafficMustang.R;
import org.liquidByte.trafficMustang.androidApp.App;
import org.liquidByte.trafficMustang.androidApp.Bindings;
import org.liquidByte.trafficMustang.androidApp.listview.adapter.PassageiroRowAdapter;
import org.liquidByte.trafficMustang.androidApp.listview.row.PassageiroRow;
import org.liquidByte.trafficMustang.androidApp.listview.row.RowActionListener;
import org.liquidByte.trafficMustang.androidApp.services.Services;
import org.liquidByte.trafficMustang.androidApp.services.TrechoService;
import org.liquidByte.trafficMustang.androidApp.services.dto.DeslocamentoDTO;
import org.liquidByte.trafficMustang.androidApp.services.dto.TrechoDTO;
import org.liquidByte.trafficMustang.androidApp.services.dto.UsuarioDTO;
import org.liquidByte.trafficMustang.androidApp.services.dto.VeiculoDTO;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

public class VisualizarDeslocamentoActivity extends ModelActivity<DeslocamentoDTO> {

	private TextView txvSituacao;
	private TextView txvTaxa;
	private TextView txvVeiculo;
	private ListView listView;
	private PassageiroRowAdapter adapter;
	
	protected TextView getTxvSituacao() {
		if (this.txvSituacao == null) {
			this.txvSituacao = (TextView)this.findViewById(R.id.activity_visualizar_deslocamento_txvSituacao);
		}
		return txvSituacao;
	}

	protected TextView getTxvTaxa() {
		if (this.txvTaxa == null) {
			this.txvTaxa = (TextView)this.findViewById(R.id.activity_visualizar_deslocamento_txvTaxa);
		}
		return txvTaxa;
	}

	protected TextView getTxvVeiculo() {
		if (this.txvVeiculo == null) {
			this.txvVeiculo = (TextView)this.findViewById(R.id.activity_visualizar_deslocamento_txvVeiculo);
		}
		return txvVeiculo;
	}

	protected ListView getListView() {
		if (this.listView == null) {
			this.listView = (ListView)this.findViewById(R.id.activity_visualizar_deslocamento_listview);
		}
		return listView;
	}
	
	protected PassageiroRowAdapter getAdapter() {
		if (this.adapter == null) {
			this.adapter = new PassageiroRowAdapter(this);
		}
		return adapter;
	}

	public VisualizarDeslocamentoActivity() {
		super(R.layout.activity_visualizar_deslocamento, DeslocamentoDTO.class, false);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.getAdapter().setLocalizacaoActionListener(new RowActionListener<PassageiroRow>() {
			@Override
			public void actionPerformed(PassageiroRow row) {
				VisualizarDeslocamentoActivity.this.rastrearUsuario(row.getModel());
			}
		});
		
		this.getListView().setAdapter(this.getAdapter());
	}

	@Override
	protected void bindToView() {
		DeslocamentoDTO model = this.getModel();
		VeiculoDTO veiculo = model.getVeiculo();
		
		this.getTxvSituacao().setText(model.getSituacao());
		Bindings.currencySet(this.getTxvTaxa(), model.getTaxa());
		
		if (veiculo == null) {
			this.getTxvVeiculo().setText(null);
		} else {
			this.getTxvVeiculo().setText(String.format("%s (%s)", veiculo.getModelo(), veiculo.getPlaca()));
		}
		
		this.getAdapter().clear();
		TrechoService service = Services.trecho();
		try {
			TrechoDTO[] trechos = service.get(model);
			if (trechos != null && trechos.length > 0) {
				for (TrechoDTO trecho : trechos) {
					this.getAdapter().put(trecho);
				}
			}
		} catch (Exception e) {
			App.error(e);
		}
	}
	
	private void rastrearUsuario(TrechoDTO trecho) {
		Intent intent = new Intent(this, VisualizarLocalizacaoActivity.class);
		intent.putExtra(UsuarioDTO.class.getName(), trecho.getPassageiro());
		this.startActivity(intent);
	}
}
